package com.clear.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.clear.subject.application.convert.SubjectLabelDTOConverter;
import com.clear.subject.application.dto.SubjectLabelDTO;
import com.clear.subject.common.entity.Result;
import com.clear.subject.domain.entity.SubjectLabelBO;
import com.clear.subject.domain.service.SubjectLabelDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 标签Controller
 */
@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {

    @Autowired
    private SubjectLabelDomainService subjectLabelDomainService;

    /**
     * 新增标签
     *
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.add.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }
            // 参数校验
            Preconditions.checkArgument(Objects.nonNull(subjectLabelDTO), "参数不能为空！");
            Preconditions.checkArgument(!StringUtils.isBlank(subjectLabelDTO.getLabelName()), "标签名称不能为空！");
            Preconditions.checkArgument(Objects.nonNull(subjectLabelDTO.getSortNum()), "标签排序不能为空！");
            Preconditions.checkArgument(Objects.nonNull(subjectLabelDTO.getCategoryId()), "分类id不能为空！");

            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);

            Boolean result = subjectLabelDomainService.add(subjectLabelBO);

            return Result.ok(result);
        } catch (Exception e) {
            log.info("SubjectLabelController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增标签失败");
        }

    }

    /**
     * 更新标签
     *
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.update.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }
            // 参数校验：更新标签是根据id来更新，id不能为空
            Preconditions.checkArgument(Objects.nonNull(subjectLabelDTO), "参数不能为空！");
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "标签id不能为空");

            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);

            Boolean result = subjectLabelDomainService.update(subjectLabelBO);

            return Result.ok(result);
        } catch (Exception e) {
            log.info("SubjectLabelController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新标签失败");
        }
    }

    /**
     * 删除标签
     * 逻辑删除
     *
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.delete.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }
            // 参数校验：根据id来删除，id不能为空
            Preconditions.checkArgument(Objects.nonNull(subjectLabelDTO), "参数不能为空！");
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "标签id不能为空");

            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);

            Boolean result = subjectLabelDomainService.delete(subjectLabelBO);

            return Result.ok(result);
        } catch (Exception e) {
            log.info("SubjectLabelController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除标签失败");
        }

    }

    /**
     * <p>
     * 查询标签（根据分类查询标签）
     * </p>
     *
     * @param subjectLabelDTO
     * @return 标签list
     */
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.queryLabelByCategoryId.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }
            // 参数校验：根据分类id来查询标签，分类id不能为空
            Preconditions.checkArgument(Objects.nonNull(subjectLabelDTO), "参数不能为空！");
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "分类id不能为空");

            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);

            List<SubjectLabelBO> boList = subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBO);

            List<SubjectLabelDTO> dtoList = SubjectLabelDTOConverter.INSTANCE.convertLabelBOListToLabelDTOList(boList);

            return Result.ok(dtoList);
        } catch (Exception e) {
            log.info("SubjectLabelController.queryLabelByCategoryId.error:{}", e.getMessage(), e);
            return Result.fail("根据分类查询标签失败");
        }
    }
}
